﻿using DAL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Site : System.Web.UI.MasterPage
{
    DigitalWorldDataContext db = new DigitalWorldDataContext();
    string connectionString = WebConfigurationManager.ConnectionStrings["DAL.Properties.Settings.DigitalWorldConnectionString"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            rpCategories.DataSource = getCategory();
            rpCategories.DataBind();
            Session["CatID"] = null;
            TestLogined();
        }
    }

    void TestLogined()
    {
        HttpCookie ck = Request.Cookies["www.digital.com"];
        if (Session["Username"] == null)
        {
            MultiView1.SetActiveView(View1);
            if (ck == null)
                MultiView1.SetActiveView(View1);
            else
            {
                Session["Username"] = ck["Username"];
                MultiView1.SetActiveView(View2);
            }
        }
        else
            MultiView1.SetActiveView(View2);
    }

    public DataTable getCategory()
    {
        SqlConnection cnn = new SqlConnection(connectionString);
        string sql = "sp_CategoryByStatus";
        SqlCommand cmd = new SqlCommand(sql, cnn);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        adapter.Fill(dt);
        return dt;
    }
    public DataTable getSupplierByCateID(string CateID)
    {
        SqlConnection cnn = new SqlConnection(connectionString);
        string sql = "sp_SupplierByCatID";
        SqlCommand cmd = new SqlCommand(sql, cnn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("CatID", CateID);
        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        adapter.Fill(dt);
        return dt;
    }
    protected void rpCategories_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        DataRowView dr = (DataRowView)e.Item.DataItem;

        Repeater rpSup = (Repeater)e.Item.FindControl("rpSupplier");
        if (rpSup != null)
        {
            DataTable dt = getSupplierByCateID(dr["categoryname"].ToString());
            if (dt != null)
            {
                rpSup.DataSource = dt;
                rpSup.DataBind();
            }
        }
    }
    protected void lbt_Login_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Dang-Nhap.aspx");
    }
    protected void lbt_Logout_Click(object sender, EventArgs e)
    {
        Session.Abandon();
        HttpCookie ck = Request.Cookies["www.digital.com"];
        if (ck != null)
        {
            ck.Expires = DateTime.Now.AddSeconds(-1);
            Response.Cookies.Add(ck);           
        }
        Response.Redirect("~/Default.aspx");
    }
}
